<?php

    /**
     * Redirect
     *
     * Response to redirect to an action
     *
     * @package      Core
     * @subpackage   Controller_Response
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    final class Core_Controller_Response_Redirect extends Core_Controller_Response_Abstract
    {

	////////////////////////////////////////////////////////////////////////////
	//                                Variables                               //
	////////////////////////////////////////////////////////////////////////////

        /**
        * The response type
        *
        * @var string
        */
        protected $_type = 'redirect';

        /**
         * Full url to redirect
         *
         * @var string
         */
        private $_url = '';

        /**
         * Selector of the action where you want to redirect.
         * Core_Url will be used to get the real url
         *
         * @var string
         */
        private $_action = '';

        /**
         * The anchor you want to add to the final url.
         * Leave blank if you don't one.
         *
         * @var string
         */
        private $_anchor = '';

        /**
         * Parameters for the action/url
         *
         * @var array
         */
        public $params = array();

    ////////////////////////////////////////////////////////////////////////////
	//                            Getters / Setters                           //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Sets an URL
         *
         * @param       string      $value
		 * @throws		Core_ArgumentException
		 * @throws		Core_ArgumentNullException
         * @return      Core_Controller_Response_Redirect
         */
        public function setUrl($value)
        {
            if ($value === null) {
				throw new Core_ArgumentNullException('value is a null reference');
			}

            if (strlen(trim($value)) == 0) {
				throw new Core_ArgumentException('value is a zero-length string');
			}

            $this->_url = $value;
            return $this;
        }

        /**
         * Sets an anchor
         *
         * @param       string      $value
		 * @throws		Core_ArgumentException
		 * @throws		Core_ArgumentNullException
         * @return      Core_Controller_Response_Redirect
         */
        public function setAnchor($value)
        {
            if ($value === null) {
				throw new Core_ArgumentNullException('value is a null reference');
			}

            if (strlen(trim($value)) == 0) {
				throw new Core_ArgumentException('value is a zero-length string');
			}

            $this->_anchor = $value;
            return $this;
        }

        /**
         * Sets the action
         *
         * @param       string      $value
		 * @throws		Core_ArgumentException
		 * @throws		Core_ArgumentNullException
         * @return      Core_Controller_Response_Redirect
         */
        public function setAction($value)
        {
            if ($value === null) {
				throw new Core_ArgumentNullException('value is a null reference');
			}

            if (strlen(trim($value)) == 0) {
				throw new Core_ArgumentException('value is a zero-length string');
			}

            $this->_action = $value;
            return $this;
        }

	////////////////////////////////////////////////////////////////////////////
	//                                 Methods                                //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Sets the url with the referer URL
         *
         * @param       string      $defaultUrl
         * @throws		Core_ArgumentNullException
         * @return      boolean     true if there is a referer URL
         */
        public function toReferer($defaultUrl = '')
        {
            if ($defaultUrl === null) {
				throw new Core_ArgumentNullException('defaultUrl is a null reference');
			}

            if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] != '') {
                $this->_url = $_SERVER['HTTP_REFERER'];
                return true;
            } else {
                $this->_url = $defaultUrl;
                return false;
            }
        }

        /**
         * Sends the HTTP header location instruction
         *
         * @return      boolean    true if no errors
         */
        public function output()
        {
            if ($this->hasErrors()) {
                return false;
            }

            $this->sendHttpHeaders();

            if ($this->_url != '') { // Full URL redirection
                header('Location: '. $this->_url);
            } else { // Anchor redirection
                switch ($this->_registry['end']) {
                    case 'front':
                        header('Location: '. DbPage::getUrl($this->_action, $this->params) . ($this->_anchor != '' ? '#' . $this->_anchor : ''));
                        break;

                    case 'admin':
                    default:
                        header('Location: '. Core_Url::get($this->_action, $this->params) . ($this->_anchor != '' ? '#' . $this->_anchor : ''));
                        break;
                }
            }

            return true;
        }

        /**
         * Outputs the errors
         *
         * @return      void
         */
        public function outputErrors()
        {
            $response = new Core_Controller_Response_Http();
            $response->outputErrors();
        }
    }
